# -*- coding:utf-8 -*-
import os,sys
import re
import traceback
import time
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), os.pardir))
import supeanut_config
from CommonLib.mylog import mylog
from CommonLib.db.MongoDBTool import MongoDBTool
from CommonLib.FileTool.CsvTool import CsvTool


'''
作者：supeanut
创建时间：2016-xx-xx xx:xx:xx
功能描述：
	空库：全量insert
	补缺：指定日期insert
相关配置：
	supeanut_config.XXX
历史改动：
	2016-xx-xx: xxxxxx
'''
class WsHistDaySend:
	# init what
	def __init__(self):
		self.mongo = MongoDBTool()
		self.csvtool = CsvTool()
		self.db_name = supeanut_config.MONGO_DB
		self.coll_daydata = supeanut_config.MONGO_COLL_DAYDATA
		self.coll_stockinfo = supeanut_config.MONGO_COLL_STOCKINFO
		self.coll_trade_calendar = supeanut_config.MONGO_COLL_TRADE_CALENDAR
		self.ws_path = supeanut_config.WS_DES_DAYDATA_CSV_PATH
	
	def insert_all(self, ):
		flag, msg = self.mongo.conn(self.db_name, self.coll_daydata)
		if flag is False:
			return False, msg
		log = mylog('WsHistDaySend.insert_all', None)
		filename_list = os.listdir(self.ws_path)
		for filename in filename_list:
			if filename.find(r'.swp') >= 0:
				continue
			print filename
			flag,records = self.csvtool.read_csv(self.ws_path, filename)
			if flag is False:
				log.error('read file %s failed'%filename)		
				continue
			docs = []
			for record in records:	
				doc = {}
				# 与配置文件相关
				doc[u'code'] = record[0].decode('utf-8')
				doc[u'date'] = record[1].decode('utf-8')
				doc[u'open'] = record[2].decode('utf-8')
				doc[u'high'] = record[3].decode('utf-8')
				doc[u'low'] = record[4].decode('utf-8')
				doc[u'close'] = record[5].decode('utf-8')
				doc[u'vol_stock'] = str(float(record[6])*100).decode('utf-8')
				doc[u'vol_capital'] = record[7].decode('utf-8')
				doc[u'datetime'] = doc[u'date']+ u' 00:00:00'
				docs.append(doc)
			try:
				# insert_many方法，适合第一次导入数据库
				self.mongo.coll.insert_many(docs)
			except:
				log.error(traceback.format_exc())
				continue
		return True, 'suc'
		

	# 插入(更新)指定日期的数据, 
	def upsert_date(self, date_list):
		flag, msg = self.mongo.conn(self.db_name, self.coll_daydata)
		if flag is False:
			return False, msg
		log = mylog('WsHistDaySend.insert_date', None)
		filename_list = os.listdir(self.ws_path)
		for filename in filename_list:
			if filename.find(r'.swp') >= 0:
				continue
			print filename
			flag,records = self.csvtool.read_csv(self.ws_path, filename)
			if flag is False:
				log.error('read file %s failed'%filename)
				continue
			for record in records:
				date = record[1]
				if date in date_list:
					doc = {}
					code = record[0].decode('utf-8')
					date = record[1].decode('utf-8')
					doc[u'open'] = record[2].decode('utf-8')
					doc[u'high'] = record[3].decode('utf-8')
					doc[u'low'] = record[4].decode('utf-8')
					doc[u'close'] = record[5].decode('utf-8')
					doc[u'vol_stock'] = str(float(record[6])*100).decode('utf-8')
					doc[u'vol_capital'] = record[7].decode('utf-8')
					doc[u'datetime'] = date+ u' 00:00:00'
					update_suc = False
					while not update_suc:
						try:
							self.mongo.coll.update_one({u'code':code, u'date':date},\
													{'$set':doc}, upsert = True)
						except:
							log.error(traceback.format_exc())
							flag, msg = self.mongo.conn(self.db_name, self.coll_daydata)
							if flag is False:
								return False, traceback.format_exc()
							else:
								continue
						update_suc = True
		return True, 'suc'





if __name__ == '__main__':
	obj = WsHistDaySend()
	obj.insert_all()
	#obj.upsert_date(['2016-12-14', '2016-12-13'])
